#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/epoll.h>

int main()
{
    // 创建epoll实例
    int epoll_fd = epoll_create(16);
    struct epoll_event ev, events[8];
    ev.data.fd = STDIN_FILENO;
    ev.events = EPOLLIN;
    // 将文件描述符添加到epoll实例中
    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, STDIN_FILENO, &ev);

    while(1)
    {
        // 等待事件发生
        int nfds = epoll_wait(epoll_fd, events, 8, 10000);
        if (nfds == -1)
        {
            perror("epoll_wait");
            exit(1);
        }
        else if (nfds == 0)
        {
            printf("timeout\n");
            continue;
        }
        for(int i = 0; i < nfds; i++)
        {
            if(events[i].data.fd == STDIN_FILENO)
            {
                char buf[1024];
                read(STDIN_FILENO, buf, sizeof(buf));
                printf("input: %s\n", buf);
            }
        }
    }
    return 0;
}